package co.chuangdian.oflauncher;

import java.io.File;
import java.util.Date;

import org.json.JSONArray;
import org.json.JSONObject;
import android.content.Intent;
import android.os.Bundle;
import cn.onekit.ASSET;
import cn.onekit.CONFIG;
import cn.onekit.FSO;

public class SplashActivity extends OFActivity {
	final int SPLASH_DISPLAY_LENGHT = 0;

	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//
        CONFIG.set("AppPauseTime", new Date(2000,1,1).getTime()); 
		install();
		Intent mainIntent = new Intent(this, MainActivity.class);
		startActivity(mainIntent);
		finish();
	}

	void install() {
		if (CONFIG.get("alias") == null) {
			JSONObject alias = new JSONObject();
			CONFIG.set("alias", alias);
		}
		//
		
		
		
		if (CONFIG.get("lockscreen") == null) {
			JSONObject lockscreen = ASSET.loadJSON("lockscreen.json");
			CONFIG.set("lockscreen", lockscreen);
			//
			File lockscreenFolder = FSO.photos("lockscreen");
			if (!lockscreenFolder.exists()) {
				if (!lockscreenFolder.mkdirs()) {
					throw new Error("[FSO]" + lockscreenFolder.getPath());
				}
			}
				String cover = lockscreen.optString("cover");
				File coverFile = FSO.photos(cover);
				ASSET.copy(cover, coverFile.getPath());	
		}
		//
		if (CONFIG.get("setting") == null) {
			JSONObject setting = ASSET.loadJSON("setting.json");
			CONFIG.set("setting", setting);
		}
		//
		if (CONFIG.get("folders") == null) {
			JSONArray folders = ASSET.loadJSONs("folders.json");
			CONFIG.set("folders", folders);

			File foldersFolder = FSO.photos("folders");
			if (!foldersFolder.exists()) {
				if (!foldersFolder.mkdirs()) {
					throw new Error("[FSO]" + foldersFolder.getPath());
				}
			}
			for (int i = 0; i < folders.length(); i++) {
				JSONObject folder = folders.optJSONObject(i);
				String cover = folder.optString("cover");
				File coverFile = FSO.photos(cover);
				ASSET.copy(cover, coverFile.getPath());
			}
		}
		//
		if (CONFIG.get("wallpapers") == null) {
			JSONArray wallpapers = ASSET.loadJSONs("wallpapers.json");
			CONFIG.set("wallpapers", wallpapers);
			File wallpapersFolder = FSO.photos("wallpapers");
			if (!FSO.exists(wallpapersFolder.getPath())) {
				if (!wallpapersFolder.mkdir()) {
					throw new Error("[FSO]" + wallpapersFolder.getPath());
				}
			}
			for (int i = 0; i < wallpapers.length(); i++) {
				try {
					JSONObject wallpaper = wallpapers.getJSONObject(i);
					String image = wallpaper.getString("image");
					File imageFile = FSO.photos(image);
					ASSET.copy(image, imageFile.getPath());
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		//
		if (CONFIG.get("widgets") == null) {
			JSONArray systemWidgets = ASSET.loadJSONs("widgets.json");
			CONFIG.set("widgets", systemWidgets);
		}
		if (CONFIG.get("systemwidgets") == null) {
			JSONObject systemWidgets = ASSET.loadJSON("systemwidgets.json");
			CONFIG.set("systemwidgets", systemWidgets);
		}
		
		if (CONFIG.get("folders_setting") == null) {
			JSONObject folders_setting = ASSET.loadJSON("folders_setting.json");
			CONFIG.set("folders_setting", folders_setting);
		}
		
	}

}